package ink.metoo.auto.fishpi.chatroom.handler

import com.google.gson.Gson
import ink.metoo.auto.fishpi.Log
import ink.metoo.auto.fishpi.call.ChatRoomCall
import ink.metoo.auto.fishpi.chatroom.Message
import ink.metoo.auto.fishpi.chatroom.RedPacket
import ink.metoo.auto.fishpi.chatroom.handler.redpacket.RedPacketHandlerFactory

class RedPacketStatusTypeHandler : TypeHandler {

    private val gson = Gson()

    override val type: Message.Type = Message.Type.RED_PACKET_STATUS

    override fun handle(text: String, message: Message) {
        val oId = message.oId!!
        try {
            val rpMessage = ChatRoomCall.getContextMessage(oId = oId, mode = 1, size = 1).data.getOrNull(0) ?: return
            if (!rpMessage.isJsonContent) return
            RedPacketHandlerFactory.receivedStatusHandle(message, gson.fromJson(rpMessage.content, RedPacket::class.java))
        } catch (e: Exception) {
            Log.debug(e.message, e)
        }
    }

}